<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class TagsController extends AppController {

    var $name = 'Tags';
    var $components = array(
        'Session', 'Alias'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'Tags', 'Games', 'Gametags'
    );

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Tags');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Tags.title LIKE' => "%$search%",
                'Tags.id LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Tags.ordering' => 'ASC'
            ),
        );
        $data = $this->paginate('Tags');
        $this->set('total', $this->params['paging']['Tags']['count']);
        $this->set('data', $data);
        $this->set('total_inpage', sizeof($data));
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
        $this->Tags->WritetagsBlock();
        $this->Tags->setListTagsCache();
    }

    function savetags(){
        $this->layout = null;
        $form_data   =   $this->params['form'];
        $tags   = explode('_', $form_data['tags_id']);
        $conditions = array(
            "{$this->Gametags->alias}.game_id" => $form_data['gameid']
        );
        $this->Gametags->deleteAll($conditions);
        if(!empty($tags[0])){
            foreach($tags AS $tag){
                $this->Gametags->id =   null;
                $data   =   array();
                $data['game_id']   =    $form_data['gameid'];
                $data['tag_id']   =    $tag;
                $data['user_created'] = $this->Auth->user('id');
                $data['user_created_ip'] = $_SERVER['REMOTE_ADDR'];
                $this->Gametags->save($data);

            }
        }
        echo '1';
        exit;
    }
    function gettags(){
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['gameid']) && is_array($params['gameid'])) {
            $this->Games->id = $params['gameid'][0];
        } else {
            $this->Games->id = array_key_exists('gameid', $params) ? $params['gameid'] : 0;
        }
        $this->layout = null;
        $this->set('games_tags', $this->Gametags->get_all_game($this->Games->id));
        $this->set('tags', $this->Tags->get_all());
        $fields = array(
             '*'
        );
        $this->set('game',$this->Games->read($fields));
        
    }
    function remove() {
        $conditions = array(
            "{$this->Faq->alias}.{$this->Faq->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        if ($this->Faq->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d nội dung", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Tags->id = $params['cid'][0];
        } else {
            $this->Tags->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
            //build css class list
            $css_class_list = array(
                'tags-10' => 'tags-10',
                'tags-14' => 'tags-14',
                'tags-11' => 'tags-11',
                'tags-12' => 'tags-12'
            );
        $this->set('css_class_list', $css_class_list);
        if (empty($this->data)) {
            $fields = array(
                '*'
            );

            
            $this->data = $this->Tags->read($fields);
        }

    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm Tags');
                    //build css class list
            $css_class_list = array(
                'tags-10' => 'tags-10',
                'tags-14' => 'tags-14',
                'tags-11' => 'tags-11',
                'tags-12' => 'tags-12'
            );
        $this->set('css_class_list', $css_class_list);
    }

    function save() {
        $data = $this->data['Tags'];
        $data['alias'] = $this->Alias->alias($data['title']);
        if (!empty($data['id'])) {
            $data['user_modified'] = $this->Auth->user('id');
            $data['user_modified_ip'] = $_SERVER['REMOTE_ADDR'];
            
        } else {
            $data['ordering'] = $this->Tags->nextOrder();
            $data['user_created'] = $this->Auth->user('id');
            $data['user_created_ip'] = $_SERVER['REMOTE_ADDR'];
        }
        if ($this->Tags->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Tags->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('action' => 'index'));
    }
    function saveorder() {
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Tags->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('action' => 'index'));
    }
    function cancel() {
        $this->redirect(array('action' => 'index'));
    }
    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Tags->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Tags->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Tags->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Tags->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('action' => 'index'));
    }
}